package client.gui.workbox;

import info.clearthought.layout.TableLayout;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import listener.Listener;
import broadcaster.Broadcaster;

/**
 * Initially shows no content
 */

public class WorkBoxDisplay extends JFrame implements Listener {
  
  /**
   * 
   */
  private static final long serialVersionUID = 1L;

  private final WorkBoxController controller_;
  
  private final JTextField status_text_;
  
  private final JComponent filler_;
  
  private final JMenuBar menubar_;
  private final JMenu[] menus_;
  
  private void checkRep() {
    // TODO
  }
  
  public WorkBoxDisplay(String name, WorkBoxController controller) {
    super(name);
    assert name != null && controller != null;
    
    controller_ = controller;
    
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    // set control pane
    Container cp = this.getContentPane();
    
    // sets the layout using table layout
    double[][] table_size = {
        {TableLayout.FILL},
        {TableLayout.PREFERRED, TableLayout.FILL, 12.0}
    };
    
    /*
     * +--------------------+
     * |MENUBAR             |
     * +--------------------+
     * |STATUS              |
     * |0, 0                |
     * +--------------------+
     * |TABS                |
     * |0, 1                |
     * +--------------------+
     * |SESSION DISPLAY     |
     * |0, 2                |
     * |                    |
     * +--------------------+
     * |LOG                 |
     * |0, 3                |
     * +--------------------+
     */
    
    cp.setLayout(new TableLayout(table_size));
    
    // add user text
    status_text_ = new JTextField();
    cp.add(status_text_, "0, 0");
    
    // tabs
    // TODO
    // cp.add(Tabs, "0, 1");
    
    // session display
    // currently empty
    filler_ = new JLabel("None selected");
    filler_.setPreferredSize(new Dimension(400, 200));
    cp.add(filler_, "0, 1");
    
    // last log field
    // TODO
    
    // menubar
    menubar_ = new JMenuBar();
    menus_ = new JMenu[1];
    menus_[0] = new JMenu("Room");
    JMenuItem createpublic, createprivate, join;
    createpublic = new JMenuItem("New Public Room...", KeyEvent.VK_U);
    menus_[0].add(createpublic);
    
    createpublic.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        String roomname = "";
        roomname = JOptionPane.showInputDialog("Room Name");
        if (!(roomname.equals(""))) {
          controller_.requestNewRoom(roomname);
        }
      }
    });
    
    createprivate = new JMenuItem("New Private Room...", KeyEvent.VK_R);
    menus_[0].add(createprivate);
    
    createprivate.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        String roomname = "";
        roomname = JOptionPane.showInputDialog("Room Name");
        if (!(roomname.equals(""))) {
          controller_.requestNewPrivatePersistentRoom(roomname);
        }
      }
    });
    
    join = new JMenuItem("Join...", KeyEvent.VK_J);
    menus_[0].add(join);
    
    join.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        String roomname = "";
        roomname = JOptionPane.showInputDialog("Room Name");
        if (!(roomname.equals(""))) {
          controller_.requestJoinRoom(roomname);
        }
      }
    });
    
    for (JMenu menu : menus_) {
      menubar_.add(menu);
    }
    
    this.setJMenuBar(menubar_);
    
    this.pack(); 
    
    // on closing do..
    this.addWindowListener(new WindowAdapter() {

      public void windowClosing(WindowEvent e) {
        System.out.println("cleaning...");
        controller_.cleanUp();
      }
    });
    
    checkRep();
  }

  public void respond(Broadcaster broadcaster) {
    assert broadcaster != null;
    // TODO
  }
  
}
